//
//  UIImage+extension.m
//  MySinaWeiboProjectForOC
//
//  Created by apple on 2016/11/23.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "UIImage+extension.h"

@implementation UIImage (extension)


+(instancetype)imageWithScreen{
    
    UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, NO, 1);
    [[UIApplication sharedApplication].keyWindow drawViewHierarchyInRect:[UIScreen mainScreen].bounds afterScreenUpdates:YES];
    UIImage *bcImage = UIGraphicsGetImageFromCurrentImageContext();
    return bcImage;
    
}

//将图片缩放到指定宽度
-(instancetype)scaleImageWithWidth:(CGFloat)width{
    
    CGSize imageSize = self.size;
    CGFloat imageH = imageSize.height;
    CGFloat imageW = imageSize.width;
    
    if(imageW < width){
        return self;
    }
    
    CGFloat height = width / imageW * imageH;
    CGRect imageBounds = CGRectMake(0, 0, width, height);
    
    //开启图片上下文
    UIGraphicsBeginImageContextWithOptions(imageBounds.size, NO, 0);
    
    [self drawInRect:imageBounds];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图片上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

@end
